{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "a8ff8fe1a55aaeb4",
   "metadata": {
    "collapsed": false
   },
   "source": [
    "# Description 描述"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "2d8f6f0232555f5b",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-12-10T08:30:56.809953Z",
     "start_time": "2023-12-10T08:30:56.759497Z"
    },
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "# import\n",
    "import sys\n",
    "import numpy as np\n",
    "import pandas as pd\n",
    "import sklearn as sk\n",
    "import matplotlib.pyplot as plt\n",
    "import time\n",
    "sys.path.append('..')\n",
    "# from utils.matplot import histogram\n",
    "# import importlib\n",
    "# importlib.reload(utils.my_config)\n",
    "\n",
    "%load_ext autoreload\n",
    "%autoreload 2\n",
    "\n",
    "np.random.seed(42)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "fb3ccbc5",
   "metadata": {},
   "outputs": [],
   "source": [
    "sys.path.append('/Users/tianzhipeng/Documents/private/cnm/Trial-7-Lan-new/PythonTest/ML/utils')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "5308321c",
   "metadata": {},
   "outputs": [],
   "source": [
    "from my_config import CustomConfigObject\n",
    "params = CustomConfigObject()\n",
    "metrics = CustomConfigObject()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "bd55dd8e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Experiment: artifact_location='file:///Users/tianzhipeng/data/mlflows/mlruns/194531117083406055', creation_time=1702212625770, experiment_id='194531117083406055', last_update_time=1702212625770, lifecycle_stage='active', name='watermark_classifier_skl', tags={}>"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import mlflow\n",
    "# mlflow.autolog() #开启autolog, 16s的svc训练过程变成了3min!!!!\n",
    "mlflow.set_tracking_uri(\"file:///Users/tianzhipeng/data/mlflows/mlruns\")\n",
    "mlflow.set_experiment(\"watermark_classifier_skl\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7178d38257f77196",
   "metadata": {
    "collapsed": false
   },
   "source": [
    "# Ingest 数据摄入"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "initial_id",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-12-10T08:30:56.810275Z",
     "start_time": "2023-12-10T08:30:56.777596Z"
    },
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "base_path=\"/Users/tianzhipeng/data/pdfres/gcjjx\"\n",
    "train_dir = f\"{base_path}/classifier\"\n",
    "test_dir = f\"{base_path}/classifier_test\"    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "ae3938d49ee1e89",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-12-10T08:31:00.350326Z",
     "start_time": "2023-12-10T08:30:56.791982Z"
    },
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "# 帮我实现一个python函数, 输入为一个目录\n",
    "# 1. 该目录下有多个子目录, 每个子目录代表一个类别, 子目录下是该类别的所有图片, 将所有子目录和图片读取\n",
    "# 2.使用PIL将图片转化为特征, 存储为训练用的X变量\n",
    "# 3.将子目录代表的标签, 存储为训练用的y变量\n",
    "# 函数返回X, y\n",
    "import os\n",
    "from PIL import Image\n",
    "import numpy as np\n",
    "\n",
    "def read_images_and_labels(directory, flatten=False, force_resize=None):\n",
    "    X = []  # 存储图像特征的列表\n",
    "    y = []  # 存储图像标签的列表\n",
    "    labels = {}  # 存储类别和对应标签的字典\n",
    "    label_index = 0  # 标签索引\n",
    "    img_shape = None\n",
    "\n",
    "    # 遍历目录下的子目录（代表类别）\n",
    "    for root, dirs, files in os.walk(directory):\n",
    "        for subdir in dirs:\n",
    "            labels[label_index] = subdir  # 将类别与索引关联\n",
    "            label_index += 1\n",
    "            subdir_path = os.path.join(root, subdir)\n",
    "\n",
    "            # 读取子目录中的图像文件\n",
    "            for file in os.listdir(subdir_path):\n",
    "                file_path = os.path.join(subdir_path, file)\n",
    "                if os.path.isfile(file_path):\n",
    "                    # 使用PIL库打开图像文件\n",
    "                    try:\n",
    "                        img = Image.open(file_path)\n",
    "                        if force_resize:\n",
    "                            img = img.resize(force_resize)\n",
    "                        else:\n",
    "                            if img_shape is None:\n",
    "                                img_shape = img.size\n",
    "                            if img.size != img_shape:\n",
    "                                raise RuntimeError(f\"图像大小不一致 {file_path} {img.size} {img_shape}\")\n",
    "                        # 将图像转换为特征向量并存储\n",
    "                        img_array = np.array(img)\n",
    "                        if flatten:\n",
    "                            feature_vector = img_array.flatten()\n",
    "                            X.append(feature_vector)\n",
    "                        else:\n",
    "                            X.append(img_array)\n",
    "                        # 存储图像对应的标签\n",
    "                        y.append(labels[label_index - 1])\n",
    "                    except Exception as e:\n",
    "                        print(f\"Error processing {file_path}: {e}\")\n",
    "    \n",
    "    # 将列表转换为NumPy数组\n",
    "    X = np.array(X)\n",
    "    y = np.array(y)\n",
    "    \n",
    "    return X, y"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "47a7dbd3dda38dcb",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-12-10T08:31:44.808043Z",
     "start_time": "2023-12-10T08:31:41.549055Z"
    },
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "params.train_dir=train_dir\n",
    "params.flatten=False\n",
    "params.force_resize=(1900, 2640)\n",
    "X, y = read_images_and_labels(params.train_dir, params.flatten, params.force_resize)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "dc4151f0bfcbf79d",
   "metadata": {
    "collapsed": false
   },
   "source": [
    "# EDA 数据探索"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "f79e6cd9173313a2",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-12-10T08:32:42.969845Z",
     "start_time": "2023-12-10T08:32:42.952200Z"
    },
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'numpy.ndarray'>\n",
      "(50, 2640, 1900, 3)\n",
      "(50,)\n"
     ]
    }
   ],
   "source": [
    "print(type(X))\n",
    "print(X.shape)\n",
    "print(y.shape)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4c827594",
   "metadata": {},
   "source": [
    "## split"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "675b8c9de6574577",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-12-10T08:33:25.972397Z",
     "start_time": "2023-12-10T08:33:25.736080Z"
    },
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(45, 2640, 1900, 3) (45,)\n",
      "(5, 2640, 1900, 3) (5,)\n"
     ]
    }
   ],
   "source": [
    "from sklearn.model_selection import train_test_split\n",
    "X_train, X_test, y_train, y_test = train_test_split(X, y,test_size=0.1, stratify=y)\n",
    "print(X_train.shape, y_train.shape)\n",
    "print(X_test.shape, y_test.shape)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "9900370a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(45, 2640, 1900, 3)"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "X_train.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "f2e18656",
   "metadata": {},
   "outputs": [],
   "source": [
    "# display(Image.fromarray(X_train[8]))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e6d0599f9f20a40",
   "metadata": {
    "collapsed": false
   },
   "source": [
    "# Modeling 建模"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d630f060",
   "metadata": {},
   "source": [
    "## SVM"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "daf56c0a64b48c24",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-12-10T08:31:14.242324Z",
     "start_time": "2023-12-10T08:31:14.225134Z"
    },
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "from sklearn.preprocessing import StandardScaler\n",
    "from sklearn.svm import SVC\n",
    "from sklearn.metrics import accuracy_score"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "397c895a",
   "metadata": {},
   "source": [
    "### 特征标签处理"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "975955e6",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[-1 -1  1 -1 -1]\n"
     ]
    }
   ],
   "source": [
    "#svm要求X标准化, 要求标签正负1\n",
    "params.standrise_y=True\n",
    "\n",
    "def transform_y(y_data):\n",
    "    return np.array([1 if y == \"1\" or y == 1 else -1 for y in y_data])\n",
    "\n",
    "if params.standrise_y:\n",
    "    y_train=transform_y(y_train)\n",
    "    y_test=transform_y(y_test)\n",
    "    print(y_test)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "da60a18d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(45, 15048000)\n",
      "(5, 15048000)\n"
     ]
    }
   ],
   "source": [
    "params.flattern_standrise_x=True\n",
    "\n",
    "def transform_x(x_data):\n",
    "    x_data = x_data.reshape(x_data.shape[0], -1)\n",
    "    print(x_data.shape)\n",
    "    scaler = StandardScaler()\n",
    "    x_data = scaler.fit_transform(x_data)\n",
    "    return x_data\n",
    "\n",
    "if params.flattern_standrise_x:\n",
    "    X_train = transform_x(X_train)\n",
    "    X_test = transform_x(X_test)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "a6b89e2e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(45, 15048000) (45,)\n"
     ]
    }
   ],
   "source": [
    "print(X_train.shape, y_train.shape)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "eab9ba3d",
   "metadata": {},
   "source": [
    "### 训练"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "59151672",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<function mlflow.sklearn.autolog(log_input_examples=False, log_model_signatures=True, log_models=True, log_datasets=True, disable=False, exclusive=False, disable_for_unsupported_versions=False, silent=False, max_tuning_runs=5, log_post_training_metrics=True, serialization_format='cloudpickle', registered_model_name=None, pos_label=None, extra_tags=None)>"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "mlflow.sklearn.autolog()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "59d84056",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<style>#sk-container-id-2 {color: black;}#sk-container-id-2 pre{padding: 0;}#sk-container-id-2 div.sk-toggleable {background-color: white;}#sk-container-id-2 label.sk-toggleable__label {cursor: pointer;display: block;width: 100%;margin-bottom: 0;padding: 0.3em;box-sizing: border-box;text-align: center;}#sk-container-id-2 label.sk-toggleable__label-arrow:before {content: \"▸\";float: left;margin-right: 0.25em;color: #696969;}#sk-container-id-2 label.sk-toggleable__label-arrow:hover:before {color: black;}#sk-container-id-2 div.sk-estimator:hover label.sk-toggleable__label-arrow:before {color: black;}#sk-container-id-2 div.sk-toggleable__content {max-height: 0;max-width: 0;overflow: hidden;text-align: left;background-color: #f0f8ff;}#sk-container-id-2 div.sk-toggleable__content pre {margin: 0.2em;color: black;border-radius: 0.25em;background-color: #f0f8ff;}#sk-container-id-2 input.sk-toggleable__control:checked~div.sk-toggleable__content {max-height: 200px;max-width: 100%;overflow: auto;}#sk-container-id-2 input.sk-toggleable__control:checked~label.sk-toggleable__label-arrow:before {content: \"▾\";}#sk-container-id-2 div.sk-estimator input.sk-toggleable__control:checked~label.sk-toggleable__label {background-color: #d4ebff;}#sk-container-id-2 div.sk-label input.sk-toggleable__control:checked~label.sk-toggleable__label {background-color: #d4ebff;}#sk-container-id-2 input.sk-hidden--visually {border: 0;clip: rect(1px 1px 1px 1px);clip: rect(1px, 1px, 1px, 1px);height: 1px;margin: -1px;overflow: hidden;padding: 0;position: absolute;width: 1px;}#sk-container-id-2 div.sk-estimator {font-family: monospace;background-color: #f0f8ff;border: 1px dotted black;border-radius: 0.25em;box-sizing: border-box;margin-bottom: 0.5em;}#sk-container-id-2 div.sk-estimator:hover {background-color: #d4ebff;}#sk-container-id-2 div.sk-parallel-item::after {content: \"\";width: 100%;border-bottom: 1px solid gray;flex-grow: 1;}#sk-container-id-2 div.sk-label:hover label.sk-toggleable__label {background-color: #d4ebff;}#sk-container-id-2 div.sk-serial::before {content: \"\";position: absolute;border-left: 1px solid gray;box-sizing: border-box;top: 0;bottom: 0;left: 50%;z-index: 0;}#sk-container-id-2 div.sk-serial {display: flex;flex-direction: column;align-items: center;background-color: white;padding-right: 0.2em;padding-left: 0.2em;position: relative;}#sk-container-id-2 div.sk-item {position: relative;z-index: 1;}#sk-container-id-2 div.sk-parallel {display: flex;align-items: stretch;justify-content: center;background-color: white;position: relative;}#sk-container-id-2 div.sk-item::before, #sk-container-id-2 div.sk-parallel-item::before {content: \"\";position: absolute;border-left: 1px solid gray;box-sizing: border-box;top: 0;bottom: 0;left: 50%;z-index: -1;}#sk-container-id-2 div.sk-parallel-item {display: flex;flex-direction: column;z-index: 1;position: relative;background-color: white;}#sk-container-id-2 div.sk-parallel-item:first-child::after {align-self: flex-end;width: 50%;}#sk-container-id-2 div.sk-parallel-item:last-child::after {align-self: flex-start;width: 50%;}#sk-container-id-2 div.sk-parallel-item:only-child::after {width: 0;}#sk-container-id-2 div.sk-dashed-wrapped {border: 1px dashed gray;margin: 0 0.4em 0.5em 0.4em;box-sizing: border-box;padding-bottom: 0.4em;background-color: white;}#sk-container-id-2 div.sk-label label {font-family: monospace;font-weight: bold;display: inline-block;line-height: 1.2em;}#sk-container-id-2 div.sk-label-container {text-align: center;}#sk-container-id-2 div.sk-container {/* jupyter's `normalize.less` sets `[hidden] { display: none; }` but bootstrap.min.css set `[hidden] { display: none !important; }` so we also need the `!important` here to be able to override the default hidden behavior on the sphinx rendered scikit-learn.org. See: https://github.com/scikit-learn/scikit-learn/issues/21755 */display: inline-block !important;position: relative;}#sk-container-id-2 div.sk-text-repr-fallback {display: none;}</style><div id=\"sk-container-id-2\" class=\"sk-top-container\"><div class=\"sk-text-repr-fallback\"><pre>SVC()</pre><b>In a Jupyter environment, please rerun this cell to show the HTML representation or trust the notebook. <br />On GitHub, the HTML representation is unable to render, please try loading this page with nbviewer.org.</b></div><div class=\"sk-container\" hidden><div class=\"sk-item\"><div class=\"sk-estimator sk-toggleable\"><input class=\"sk-toggleable__control sk-hidden--visually\" id=\"sk-estimator-id-2\" type=\"checkbox\" checked><label for=\"sk-estimator-id-2\" class=\"sk-toggleable__label sk-toggleable__label-arrow\">SVC</label><div class=\"sk-toggleable__content\"><pre>SVC()</pre></div></div></div></div></div>"
      ],
      "text/plain": [
       "SVC()"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "classifier = SVC() # 训练16s 预测31s, 准确率0.911\n",
    "classifier.fit(X_train, y_train)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "ae1c4c81",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.8666666666666667"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "y_train_pred = classifier.predict(X_train)  #训练16s 预测31s, 准确率0.911\n",
    "accuracy = accuracy_score(y_train, y_train_pred)\n",
    "accuracy"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1ac7e68c",
   "metadata": {},
   "source": [
    "### 在这里测试了一下MLFlow相关的"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "986de31f",
   "metadata": {},
   "outputs": [],
   "source": [
    "mlflow.end_run()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "da41d3f8",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<ActiveRun: >"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "mlflow.end_run()\n",
    "mlflow.active_run = None\n",
    "mlflow.start_run(run_name=None)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "28e64f40",
   "metadata": {},
   "outputs": [],
   "source": [
    "params.a=1\n",
    "mlflow.log_params(params)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "0cf52000",
   "metadata": {},
   "outputs": [],
   "source": [
    "y_train_pred = classifier.predict(X_train)  \n",
    "accuracy = accuracy_score(y_train, y_train_pred)\n",
    "metrics.accuracy = accuracy\n",
    "mlflow.log_metrics(metrics)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "a1f8cd77",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<module 'mlflow.sklearn' from '/Users/tianzhipeng/Documents/env/miniconda3/lib/python3.11/site-packages/mlflow/sklearn/__init__.py'>"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "mlflow.sklearn"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "1e03fcea",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<mlflow.models.model.ModelInfo at 0x33a564290>"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "signature = mlflow.models.infer_signature(X_train[0:1], y_train_pred[0:1])\n",
    "mlflow.sklearn.log_model(\n",
    "        sk_model=classifier, \n",
    "        artifact_path=\"model\",\n",
    "        signature = signature,\n",
    "        input_example=X_train[0:1],\n",
    "        conda_env={\"dependencies\":[]}\n",
    "    )"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "5c8cfda9",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pickle\n",
    "with open('mlp_model.pkl', 'wb') as file:\n",
    "    pickle.dump(classifier, file)\n",
    "\n",
    "with open('mlp_model_input.pkl', 'wb') as file:\n",
    "    pickle.dump(X_train, file)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "id": "bf36f6b8",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "2024/10/16 14:56:39 WARNING mlflow.utils.autologging_utils: MLflow autologging encountered a warning: \"/Users/tianzhipeng/Documents/env/miniconda3/lib/python3.11/site-packages/_distutils_hack/__init__.py:18: UserWarning: Distutils was imported before Setuptools, but importing Setuptools also replaces the `distutils` module in `sys.modules`. This may lead to undesirable behaviors or errors. To avoid these issues, avoid using distutils directly, ensure that setuptools is installed in the traditional way (e.g. not an editable install), and/or make sure that setuptools is always imported before distutils.\"\n",
      "2024/10/16 14:56:39 WARNING mlflow.utils.autologging_utils: MLflow autologging encountered a warning: \"/Users/tianzhipeng/Documents/env/miniconda3/lib/python3.11/site-packages/_distutils_hack/__init__.py:33: UserWarning: Setuptools is replacing distutils.\"\n"
     ]
    },
    {
     "ename": "AssertionError",
     "evalue": "/Users/tianzhipeng/Documents/env/miniconda3/lib/python3.11/distutils/core.py",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mAssertionError\u001b[0m                            Traceback (most recent call last)",
      "Cell \u001b[0;32mIn[33], line 2\u001b[0m\n\u001b[1;32m      1\u001b[0m \u001b[38;5;28;01mwith\u001b[39;00m mlflow\u001b[38;5;241m.\u001b[39mstart_run(run_name\u001b[38;5;241m=\u001b[39m\u001b[38;5;28;01mNone\u001b[39;00m):\n\u001b[0;32m----> 2\u001b[0m     \u001b[43mmlflow\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43msklearn\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mautolog\u001b[49m\u001b[43m(\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m      3\u001b[0m     classifier \u001b[38;5;241m=\u001b[39m SVC()\n\u001b[1;32m      4\u001b[0m     classifier\u001b[38;5;241m.\u001b[39mfit(X_train, y_train)\n",
      "File \u001b[0;32m~/Documents/env/miniconda3/lib/python3.11/site-packages/mlflow/utils/autologging_utils/__init__.py:424\u001b[0m, in \u001b[0;36mautologging_integration.<locals>.wrapper.<locals>.autolog\u001b[0;34m(*args, **kwargs)\u001b[0m\n\u001b[1;32m    405\u001b[0m \u001b[38;5;28;01mwith\u001b[39;00m set_mlflow_events_and_warnings_behavior_globally(\n\u001b[1;32m    406\u001b[0m     \u001b[38;5;66;03m# MLflow warnings emitted during autologging setup / enablement are likely\u001b[39;00m\n\u001b[1;32m    407\u001b[0m     \u001b[38;5;66;03m# actionable and relevant to the user, so they should be emitted as normal\u001b[39;00m\n\u001b[0;32m   (...)\u001b[0m\n\u001b[1;32m    420\u001b[0m     disable_warnings\u001b[38;5;241m=\u001b[39mis_silent_mode,\n\u001b[1;32m    421\u001b[0m ):\n\u001b[1;32m    422\u001b[0m     _check_and_log_warning_for_unsupported_package_versions(name)\n\u001b[0;32m--> 424\u001b[0m     \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[43m_autolog\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43margs\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43mkwargs\u001b[49m\u001b[43m)\u001b[49m\n",
      "File \u001b[0;32m~/Documents/env/miniconda3/lib/python3.11/site-packages/mlflow/sklearn/__init__.py:1261\u001b[0m, in \u001b[0;36mautolog\u001b[0;34m(log_input_examples, log_model_signatures, log_models, log_datasets, disable, exclusive, disable_for_unsupported_versions, silent, max_tuning_runs, log_post_training_metrics, serialization_format, registered_model_name, pos_label, extra_tags)\u001b[0m\n\u001b[1;32m    976\u001b[0m \u001b[38;5;129m@autologging_integration\u001b[39m(FLAVOR_NAME)\n\u001b[1;32m    977\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21mautolog\u001b[39m(\n\u001b[1;32m    978\u001b[0m     log_input_examples\u001b[38;5;241m=\u001b[39m\u001b[38;5;28;01mFalse\u001b[39;00m,\n\u001b[0;32m   (...)\u001b[0m\n\u001b[1;32m    991\u001b[0m     extra_tags\u001b[38;5;241m=\u001b[39m\u001b[38;5;28;01mNone\u001b[39;00m,\n\u001b[1;32m    992\u001b[0m ):  \u001b[38;5;66;03m# pylint: disable=unused-argument\u001b[39;00m\n\u001b[1;32m    993\u001b[0m \u001b[38;5;250m    \u001b[39m\u001b[38;5;124;03m\"\"\"\u001b[39;00m\n\u001b[1;32m    994\u001b[0m \u001b[38;5;124;03m    Enables (or disables) and configures autologging for scikit-learn estimators.\u001b[39;00m\n\u001b[1;32m    995\u001b[0m \n\u001b[0;32m   (...)\u001b[0m\n\u001b[1;32m   1259\u001b[0m \u001b[38;5;124;03m    :param extra_tags: A dictionary of extra tags to set on each managed run created by autologging.\u001b[39;00m\n\u001b[1;32m   1260\u001b[0m \u001b[38;5;124;03m    \"\"\"\u001b[39;00m\n\u001b[0;32m-> 1261\u001b[0m     \u001b[43m_autolog\u001b[49m\u001b[43m(\u001b[49m\n\u001b[1;32m   1262\u001b[0m \u001b[43m        \u001b[49m\u001b[43mflavor_name\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mFLAVOR_NAME\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m   1263\u001b[0m \u001b[43m        \u001b[49m\u001b[43mlog_input_examples\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mlog_input_examples\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m   1264\u001b[0m \u001b[43m        \u001b[49m\u001b[43mlog_model_signatures\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mlog_model_signatures\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m   1265\u001b[0m \u001b[43m        \u001b[49m\u001b[43mlog_models\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mlog_models\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m   1266\u001b[0m \u001b[43m        \u001b[49m\u001b[43mlog_datasets\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mlog_datasets\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m   1267\u001b[0m \u001b[43m        \u001b[49m\u001b[43mdisable\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mdisable\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m   1268\u001b[0m \u001b[43m        \u001b[49m\u001b[43mexclusive\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mexclusive\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m   1269\u001b[0m \u001b[43m        \u001b[49m\u001b[43mdisable_for_unsupported_versions\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mdisable_for_unsupported_versions\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m   1270\u001b[0m \u001b[43m        \u001b[49m\u001b[43msilent\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43msilent\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m   1271\u001b[0m \u001b[43m        \u001b[49m\u001b[43mmax_tuning_runs\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mmax_tuning_runs\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m   1272\u001b[0m \u001b[43m        \u001b[49m\u001b[43mlog_post_training_metrics\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mlog_post_training_metrics\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m   1273\u001b[0m \u001b[43m        \u001b[49m\u001b[43mserialization_format\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mserialization_format\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m   1274\u001b[0m \u001b[43m        \u001b[49m\u001b[43mpos_label\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mpos_label\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m   1275\u001b[0m \u001b[43m        \u001b[49m\u001b[43mextra_tags\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mextra_tags\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m   1276\u001b[0m \u001b[43m    \u001b[49m\u001b[43m)\u001b[49m\n",
      "File \u001b[0;32m~/Documents/env/miniconda3/lib/python3.11/site-packages/mlflow/sklearn/__init__.py:1822\u001b[0m, in \u001b[0;36m_autolog\u001b[0;34m(flavor_name, log_input_examples, log_model_signatures, log_models, log_datasets, disable, exclusive, disable_for_unsupported_versions, silent, max_tuning_runs, log_post_training_metrics, serialization_format, pos_label, extra_tags)\u001b[0m\n\u001b[1;32m   1820\u001b[0m     allow_children_patch \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;01mTrue\u001b[39;00m\n\u001b[1;32m   1821\u001b[0m \u001b[38;5;28;01melse\u001b[39;00m:\n\u001b[0;32m-> 1822\u001b[0m     estimators_to_patch \u001b[38;5;241m=\u001b[39m \u001b[43m_gen_estimators_to_patch\u001b[49m\u001b[43m(\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m   1823\u001b[0m     patched_fit_impl \u001b[38;5;241m=\u001b[39m fit_mlflow\n\u001b[1;32m   1824\u001b[0m     allow_children_patch \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;01mFalse\u001b[39;00m\n",
      "File \u001b[0;32m~/Documents/env/miniconda3/lib/python3.11/site-packages/mlflow/sklearn/__init__.py:98\u001b[0m, in \u001b[0;36m_gen_estimators_to_patch\u001b[0;34m()\u001b[0m\n\u001b[1;32m     92\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21m_gen_estimators_to_patch\u001b[39m():\n\u001b[1;32m     93\u001b[0m     \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01mmlflow\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01msklearn\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mutils\u001b[39;00m \u001b[38;5;28;01mimport\u001b[39;00m (\n\u001b[1;32m     94\u001b[0m         _all_estimators,\n\u001b[1;32m     95\u001b[0m         _get_meta_estimators_for_autologging,\n\u001b[1;32m     96\u001b[0m     )\n\u001b[0;32m---> 98\u001b[0m     _, estimators_to_patch \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mzip\u001b[39m(\u001b[38;5;241m*\u001b[39m\u001b[43m_all_estimators\u001b[49m\u001b[43m(\u001b[49m\u001b[43m)\u001b[49m)\n\u001b[1;32m     99\u001b[0m     \u001b[38;5;66;03m# Ensure that relevant meta estimators (e.g. GridSearchCV, Pipeline) are selected\u001b[39;00m\n\u001b[1;32m    100\u001b[0m     \u001b[38;5;66;03m# for patching if they are not already included in the output of `all_estimators()`\u001b[39;00m\n\u001b[1;32m    101\u001b[0m     estimators_to_patch \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mset\u001b[39m(estimators_to_patch)\u001b[38;5;241m.\u001b[39munion(\n\u001b[1;32m    102\u001b[0m         \u001b[38;5;28mset\u001b[39m(_get_meta_estimators_for_autologging())\n\u001b[1;32m    103\u001b[0m     )\n",
      "File \u001b[0;32m~/Documents/env/miniconda3/lib/python3.11/site-packages/mlflow/sklearn/utils.py:858\u001b[0m, in \u001b[0;36m_all_estimators\u001b[0;34m()\u001b[0m\n\u001b[1;32m    855\u001b[0m \u001b[38;5;28;01mtry\u001b[39;00m:\n\u001b[1;32m    856\u001b[0m     \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01msklearn\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mutils\u001b[39;00m \u001b[38;5;28;01mimport\u001b[39;00m all_estimators\n\u001b[0;32m--> 858\u001b[0m     \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[43mall_estimators\u001b[49m\u001b[43m(\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m    859\u001b[0m \u001b[38;5;28;01mexcept\u001b[39;00m \u001b[38;5;167;01mImportError\u001b[39;00m:\n\u001b[1;32m    860\u001b[0m     \u001b[38;5;28;01mreturn\u001b[39;00m _backported_all_estimators()\n",
      "File \u001b[0;32m~/Documents/env/miniconda3/lib/python3.11/site-packages/sklearn/utils/discovery.py:63\u001b[0m, in \u001b[0;36mall_estimators\u001b[0;34m(type_filter)\u001b[0m\n\u001b[1;32m     60\u001b[0m \u001b[38;5;66;03m# Ignore deprecation warnings triggered at import time and from walking\u001b[39;00m\n\u001b[1;32m     61\u001b[0m \u001b[38;5;66;03m# packages\u001b[39;00m\n\u001b[1;32m     62\u001b[0m \u001b[38;5;28;01mwith\u001b[39;00m ignore_warnings(category\u001b[38;5;241m=\u001b[39m\u001b[38;5;167;01mFutureWarning\u001b[39;00m):\n\u001b[0;32m---> 63\u001b[0m \u001b[43m    \u001b[49m\u001b[38;5;28;43;01mfor\u001b[39;49;00m\u001b[43m \u001b[49m\u001b[43m_\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mmodule_name\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43m_\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;129;43;01min\u001b[39;49;00m\u001b[43m \u001b[49m\u001b[43mpkgutil\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mwalk_packages\u001b[49m\u001b[43m(\u001b[49m\u001b[43mpath\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43m[\u001b[49m\u001b[43mroot\u001b[49m\u001b[43m]\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mprefix\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43msklearn.\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m)\u001b[49m\u001b[43m:\u001b[49m\n\u001b[1;32m     64\u001b[0m \u001b[43m        \u001b[49m\u001b[43mmodule_parts\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43m \u001b[49m\u001b[43mmodule_name\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43msplit\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43m.\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m)\u001b[49m\n\u001b[1;32m     65\u001b[0m \u001b[43m        \u001b[49m\u001b[38;5;28;43;01mif\u001b[39;49;00m\u001b[43m \u001b[49m\u001b[43m(\u001b[49m\n\u001b[1;32m     66\u001b[0m \u001b[43m            \u001b[49m\u001b[38;5;28;43many\u001b[39;49m\u001b[43m(\u001b[49m\u001b[43mpart\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;129;43;01min\u001b[39;49;00m\u001b[43m \u001b[49m\u001b[43m_MODULE_TO_IGNORE\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;28;43;01mfor\u001b[39;49;00m\u001b[43m \u001b[49m\u001b[43mpart\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;129;43;01min\u001b[39;49;00m\u001b[43m \u001b[49m\u001b[43mmodule_parts\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m     67\u001b[0m \u001b[43m            \u001b[49m\u001b[38;5;129;43;01mor\u001b[39;49;00m\u001b[43m \u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43m._\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m \u001b[49m\u001b[38;5;129;43;01min\u001b[39;49;00m\u001b[43m \u001b[49m\u001b[43mmodule_name\u001b[49m\n\u001b[1;32m     68\u001b[0m \u001b[43m        \u001b[49m\u001b[43m)\u001b[49m\u001b[43m:\u001b[49m\n",
      "File \u001b[0;32m~/Documents/env/miniconda3/lib/python3.11/pkgutil.py:92\u001b[0m, in \u001b[0;36mwalk_packages\u001b[0;34m(path, prefix, onerror)\u001b[0m\n\u001b[1;32m     90\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m info\u001b[38;5;241m.\u001b[39mispkg:\n\u001b[1;32m     91\u001b[0m     \u001b[38;5;28;01mtry\u001b[39;00m:\n\u001b[0;32m---> 92\u001b[0m         \u001b[38;5;28m__import__\u001b[39m(info\u001b[38;5;241m.\u001b[39mname)\n\u001b[1;32m     93\u001b[0m     \u001b[38;5;28;01mexcept\u001b[39;00m \u001b[38;5;167;01mImportError\u001b[39;00m:\n\u001b[1;32m     94\u001b[0m         \u001b[38;5;28;01mif\u001b[39;00m onerror \u001b[38;5;129;01mis\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m \u001b[38;5;28;01mNone\u001b[39;00m:\n",
      "File \u001b[0;32m~/Documents/env/miniconda3/lib/python3.11/site-packages/sklearn/_build_utils/__init__.py:15\u001b[0m\n\u001b[1;32m     13\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01m_min_dependencies\u001b[39;00m \u001b[38;5;28;01mimport\u001b[39;00m CYTHON_MIN_VERSION\n\u001b[1;32m     14\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mexternals\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01m_packaging\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mversion\u001b[39;00m \u001b[38;5;28;01mimport\u001b[39;00m parse\n\u001b[0;32m---> 15\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mopenmp_helpers\u001b[39;00m \u001b[38;5;28;01mimport\u001b[39;00m check_openmp_support\n\u001b[1;32m     16\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mpre_build_helpers\u001b[39;00m \u001b[38;5;28;01mimport\u001b[39;00m basic_check_build\n\u001b[1;32m     18\u001b[0m DEFAULT_ROOT \u001b[38;5;241m=\u001b[39m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124msklearn\u001b[39m\u001b[38;5;124m\"\u001b[39m\n",
      "File \u001b[0;32m~/Documents/env/miniconda3/lib/python3.11/site-packages/sklearn/_build_utils/openmp_helpers.py:12\u001b[0m\n\u001b[1;32m      9\u001b[0m \u001b[38;5;28;01mimport\u001b[39;00m \u001b[38;5;21;01mtextwrap\u001b[39;00m\n\u001b[1;32m     10\u001b[0m \u001b[38;5;28;01mimport\u001b[39;00m \u001b[38;5;21;01mwarnings\u001b[39;00m\n\u001b[0;32m---> 12\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mpre_build_helpers\u001b[39;00m \u001b[38;5;28;01mimport\u001b[39;00m compile_test_program\n\u001b[1;32m     15\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21mget_openmp_flag\u001b[39m():\n\u001b[1;32m     16\u001b[0m     \u001b[38;5;28;01mif\u001b[39;00m sys\u001b[38;5;241m.\u001b[39mplatform \u001b[38;5;241m==\u001b[39m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mwin32\u001b[39m\u001b[38;5;124m\"\u001b[39m:\n",
      "File \u001b[0;32m~/Documents/env/miniconda3/lib/python3.11/site-packages/sklearn/_build_utils/pre_build_helpers.py:10\u001b[0m\n\u001b[1;32m      7\u001b[0m \u001b[38;5;28;01mimport\u001b[39;00m \u001b[38;5;21;01mtempfile\u001b[39;00m\n\u001b[1;32m      8\u001b[0m \u001b[38;5;28;01mimport\u001b[39;00m \u001b[38;5;21;01mtextwrap\u001b[39;00m\n\u001b[0;32m---> 10\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01msetuptools\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mcommand\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mbuild_ext\u001b[39;00m \u001b[38;5;28;01mimport\u001b[39;00m customize_compiler, new_compiler\n\u001b[1;32m     13\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21mcompile_test_program\u001b[39m(code, extra_preargs\u001b[38;5;241m=\u001b[39m\u001b[38;5;28;01mNone\u001b[39;00m, extra_postargs\u001b[38;5;241m=\u001b[39m\u001b[38;5;28;01mNone\u001b[39;00m):\n\u001b[1;32m     14\u001b[0m \u001b[38;5;250m    \u001b[39m\u001b[38;5;124;03m\"\"\"Check that some C code can be compiled and run\"\"\"\u001b[39;00m\n",
      "File \u001b[0;32m~/Documents/env/miniconda3/lib/python3.11/site-packages/setuptools/__init__.py:7\u001b[0m\n\u001b[1;32m      4\u001b[0m \u001b[38;5;28;01mimport\u001b[39;00m \u001b[38;5;21;01mos\u001b[39;00m\n\u001b[1;32m      5\u001b[0m \u001b[38;5;28;01mimport\u001b[39;00m \u001b[38;5;21;01mre\u001b[39;00m\n\u001b[0;32m----> 7\u001b[0m \u001b[38;5;28;01mimport\u001b[39;00m \u001b[38;5;21;01m_distutils_hack\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01moverride\u001b[39;00m  \u001b[38;5;66;03m# noqa: F401\u001b[39;00m\n\u001b[1;32m      9\u001b[0m \u001b[38;5;28;01mimport\u001b[39;00m \u001b[38;5;21;01mdistutils\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mcore\u001b[39;00m\n\u001b[1;32m     10\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01mdistutils\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01merrors\u001b[39;00m \u001b[38;5;28;01mimport\u001b[39;00m DistutilsOptionError\n",
      "File \u001b[0;32m~/Documents/env/miniconda3/lib/python3.11/site-packages/_distutils_hack/override.py:1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m \u001b[38;5;28;43m__import__\u001b[39;49m\u001b[43m(\u001b[49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[38;5;124;43m_distutils_hack\u001b[39;49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[43m)\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mdo_override\u001b[49m\u001b[43m(\u001b[49m\u001b[43m)\u001b[49m\n",
      "File \u001b[0;32m~/Documents/env/miniconda3/lib/python3.11/site-packages/_distutils_hack/__init__.py:77\u001b[0m, in \u001b[0;36mdo_override\u001b[0;34m()\u001b[0m\n\u001b[1;32m     75\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m enabled():\n\u001b[1;32m     76\u001b[0m     warn_distutils_present()\n\u001b[0;32m---> 77\u001b[0m     \u001b[43mensure_local_distutils\u001b[49m\u001b[43m(\u001b[49m\u001b[43m)\u001b[49m\n",
      "File \u001b[0;32m~/Documents/env/miniconda3/lib/python3.11/site-packages/_distutils_hack/__init__.py:64\u001b[0m, in \u001b[0;36mensure_local_distutils\u001b[0;34m()\u001b[0m\n\u001b[1;32m     62\u001b[0m \u001b[38;5;66;03m# check that submodules load as expected\u001b[39;00m\n\u001b[1;32m     63\u001b[0m core \u001b[38;5;241m=\u001b[39m importlib\u001b[38;5;241m.\u001b[39mimport_module(\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mdistutils.core\u001b[39m\u001b[38;5;124m'\u001b[39m)\n\u001b[0;32m---> 64\u001b[0m \u001b[38;5;28;01massert\u001b[39;00m \u001b[38;5;124m'\u001b[39m\u001b[38;5;124m_distutils\u001b[39m\u001b[38;5;124m'\u001b[39m \u001b[38;5;129;01min\u001b[39;00m core\u001b[38;5;241m.\u001b[39m\u001b[38;5;18m__file__\u001b[39m, core\u001b[38;5;241m.\u001b[39m\u001b[38;5;18m__file__\u001b[39m\n\u001b[1;32m     65\u001b[0m \u001b[38;5;28;01massert\u001b[39;00m \u001b[38;5;124m'\u001b[39m\u001b[38;5;124msetuptools._distutils.log\u001b[39m\u001b[38;5;124m'\u001b[39m \u001b[38;5;129;01mnot\u001b[39;00m \u001b[38;5;129;01min\u001b[39;00m sys\u001b[38;5;241m.\u001b[39mmodules\n",
      "\u001b[0;31mAssertionError\u001b[0m: /Users/tianzhipeng/Documents/env/miniconda3/lib/python3.11/distutils/core.py"
     ]
    }
   ],
   "source": [
    "with mlflow.start_run(run_name=None):\n",
    "    mlflow.sklearn.autolog()\n",
    "    classifier = SVC()\n",
    "    classifier.fit(X_train, y_train)\n",
    "    mlflow.log_params(params)\n",
    "\n",
    "    y_train_pred = classifier.predict(X_train)  # 训练16s 预测31s, 准确率0.911\n",
    "    accuracy = accuracy_score(y_train, y_train_pred)\n",
    "    metrics.accuracy = accuracy\n",
    "    mlflow.log_metrics(metrics)\n",
    "\n",
    "    signature = mlflow.models.infer_signature(X_train, y_train_pred)\n",
    "\n",
    "    mlflow.sklearn.log_model(\n",
    "        sk_model=classifier, \n",
    "        artifact_path=\"iris_model\",\n",
    "        input_example=X_train,\n",
    "        signature = signature\n",
    "    )"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f4eb52d6",
   "metadata": {},
   "source": [
    "## MLP"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "id": "21ab60ca",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<ActiveRun: >"
      ]
     },
     "execution_count": 45,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "mlflow.end_run()\n",
    "mlflow.active_run = None\n",
    "mlflow.start_run(run_name=None)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "28bdb2e4",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "336.3771479129791\n"
     ]
    }
   ],
   "source": [
    "params.solver='lbfgs'\n",
    "params.alpha=1e-5\n",
    "params.hidden_layer_sizes=(5, 2)\n",
    "start=time.time()\n",
    "\n",
    "from sklearn.neural_network import MLPClassifier\n",
    "classifier = MLPClassifier(solver=params.solver, alpha=params.alpha, hidden_layer_sizes=params.hidden_layer_sizes)\n",
    "classifier.fit(X_train, y_train)  #6分半, 且内存拉满\n",
    "\n",
    "end=time.time()\n",
    "print(end-start)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "a9217eeb",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.9111111111111111"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "\n",
    "y_train_pred = classifier.predict(X_train) # 预测贼快, 准确率1.0\n",
    "accuracy = accuracy_score(y_train, y_train_pred)\n",
    "accuracy"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "924310c7",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/Users/tianzhipeng/Documents/env/miniconda3/lib/python3.11/site-packages/_distutils_hack/__init__.py:18: UserWarning: Distutils was imported before Setuptools, but importing Setuptools also replaces the `distutils` module in `sys.modules`. This may lead to undesirable behaviors or errors. To avoid these issues, avoid using distutils directly, ensure that setuptools is installed in the traditional way (e.g. not an editable install), and/or make sure that setuptools is always imported before distutils.\n",
      "  warnings.warn(\n",
      "/Users/tianzhipeng/Documents/env/miniconda3/lib/python3.11/site-packages/_distutils_hack/__init__.py:33: UserWarning: Setuptools is replacing distutils.\n",
      "  warnings.warn(\"Setuptools is replacing distutils.\")\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "<mlflow.models.model.ModelInfo at 0x3a3744910>"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "metrics.accuracy=accuracy\n",
    "metrics.train_time=end-start\n",
    "\n",
    "mlflow.log_metrics(metrics)\n",
    "mlflow.log_params(params)\n",
    "mlflow.sklearn.log_model(\n",
    "        sk_model=classifier, \n",
    "        artifact_path=\"model\",\n",
    "        input_example=X_train[0:1],\n",
    "        signature = signature,\n",
    "        conda_env={\"dependencies\":[]}\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "73c1fc3439698ff5",
   "metadata": {
    "collapsed": false
   },
   "source": [
    "# Conclusion 结论结果"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "55fe62d6c3e193fb",
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
